package ink.ykb.demo.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ink.ykb.demo.config.CodeConstant;
import ink.ykb.demo.config.MessageUtil;
import ink.ykb.demo.config.ResponseMsg;
import ink.ykb.demo.permission.annotation.Function;
import ink.ykb.demo.permission.entity.RoleRule;
import ink.ykb.demo.permission.entity.Rule;
import ink.ykb.demo.permission.service.RoleRuleService;

/**
 * 角色权限相关服务
 * 
 * @author mading
 *
 */
@RestController
@RequestMapping("/role/rule")
public class RoleRuleController {

  @Autowired
  private RoleRuleService roleRuleService;
  @Autowired
  private MessageUtil messageUtil;

  /**
   * 查询角色的所有权限列表
   * 
   * @return
   */
  @Function("admin.role.rule.list")
  @GetMapping(value = "/list")
  public ResponseMsg<List<Rule>> list(@RequestParam("roleId") Long roleId) {
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
        roleRuleService.getRuleListByRole(roleId));
  }
  
  /**
   * 角色添加权限
   * @param roleRule 参数：roleId,ruleId
   * @return
   */
  @Function("admin.role.rule.add")
  @PostMapping(value = "/add")
  public ResponseMsg<String> add(RoleRule roleRule) {
    roleRuleService.add(roleRule);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }
  
  /**
   * 角色批量添加权限
   * @param roleId
   * @param ruleIds
   * @return
   */
  @Function("admin.role.rule.add.batch")
  @PostMapping(value = "/addBatch")
  public ResponseMsg<String> addBatch(@RequestParam("roleId") Long roleId,@RequestParam("ruleIds") String ruleIds) {
    roleRuleService.addBatchRoleRule(roleId, ruleIds);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }
  
  /**
   * 角色删除权限
   * @param roleId
   * @param ruleId
   * @return
   */
  @Function("admin.role.rule.detele")
  @PostMapping(value = "/detele")
  public ResponseMsg<String> detele(@RequestParam("roleId") Long roleId,@RequestParam("ruleId") Long ruleId) {
    roleRuleService.deleteByRoleIdAndRuleId(roleId, ruleId);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }
  
  /**
   * 角色批量删除权限
   * @param roleId
   * @param ruleIds
   * @return
   */
  @Function("admin.role.rule.detele.batch")
  @PostMapping(value = "/deteleBatch")
  public ResponseMsg<String> deteleBatch(@RequestParam("roleId") Long roleId,@RequestParam("ruleIds") String ruleIds) {
    roleRuleService.deleteBatchRoleRule(roleId, ruleIds);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }
  
  /**
   * 角色批量更新权限
   * @param roleId
   * @param ruleIds
   * @return
   */
  @Function("admin.role.rule.update.batch")
  @PostMapping(value = "/updateBatch")
  public ResponseMsg<String> updateBatch(@RequestParam("roleId") Long roleId,@RequestParam("ruleIds") String ruleIds) {
    roleRuleService.updateBatchRoleRule(roleId, ruleIds);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  
  
  
  
  
  
}
